/**
 * File name: IOCommon.java Author: Willard Wang Create date: Jul 17, 2013
 */
package edu.sjtu.se.prophet.io;

import java.util.ArrayList;
import java.util.List;

import edu.sjtu.se.prophet.graph.Graph;

/**
 * @author deathspeeder
 * 
 */
public class IOCommon {

    public static String BUG_COUNT = "BugCount";
    public static String VERSION = "Version";

    protected List<Graph> graphs = new ArrayList<Graph>();
    protected List<String> uris = new ArrayList<String>();

    /**
     * @param version
     * @return
     */
    protected Graph getGraphOfVersion(String version) {
        if (version == null)
            return null;
        for (int i = 0; i < graphs.size(); i++) {
            if (version.equals(graphs.get(i).getMeta().get(VERSION)))
                return graphs.get(i);
        }
        return null;
    }

    /**
     * <ul>
     * <li>false,
     * "com.google.zxing.client.j2me.ZXingMIDlet.showError(java.lang.String)",
     * "com.google.zxing.BarcodeFormat.<init>(String)"</li>
     * 
     * <li>false,"com.google.zxing.qrcode.detector.FinderPattern.getX()",
     * "com.google.zxing.BarcodeFormat.toString()"</li>
     * 
     * <li>true,"com.J.J()", "com.J.<init>()"</li>
     * <li>true,"com.J.J", "com.J.<init>"</li>
     * <li>true,"com.J.J(int[])", "com.J.<init>(int[])"</li>
     * <li>true,"com.J.J(java.lang.String[])", "com.J.<init>(String[])"</li>
     * <li>true,"com.J.x()", "com.J.x()"</li>
     * <li>true,"com.J.x(com.J, com.J, int)", "com.J.x(J,J,int)"</li>
     * <li>true,"com.J.x(List<String>)", "com.J.x(List<String>)"</li>
     * <li>true,"com.J.x()", "com.J.x()"</li>
     * </ul>
     * 
     * @param id
     * @param method
     * @return
     */
    protected static boolean fuzzyMatch(String nodeId, String method) {
        if (nodeId.equals(method))
            return true;

        nodeId = nodeId.replaceAll(" ", "");
        String[] nodeTokens = nodeId.split("[(]");
        String[] methodTokens = method.split("[(]");

        if (nodeTokens.length != methodTokens.length)
            return false;

        if (nodeTokens.length == 0)
            return true;

        if (methodTokens[0].endsWith("<init>")) {
            String[] nodePackage = nodeTokens[0].split("[.]");
            String[] methodPackage = methodTokens[0].split("[.]");
            if (nodePackage.length != methodPackage.length)
                return false;
            for (int i = 0; i < nodePackage.length - 1; i++) {
                if (!nodePackage[i].equals(methodPackage[i]))
                    return false;
            }
            if (nodePackage.length > 1
                    && nodePackage[nodePackage.length - 1]
                            .equals(nodePackage[nodePackage.length - 2]))
                return true;
            else
                return false;
        }
        if (!nodeTokens[0].equals(methodTokens[0]))
            return false;

        if (nodeTokens.length > 1) {

            String nodePara = nodeTokens[1].replaceAll("[)]", "");
            String methodPara = methodTokens[1].replaceAll("[)]", "");

            if (nodePara.equals(methodPara))
                return true;

            String[] nodeParas = nodePara.split(",");
            String[] methodParas = methodPara.split(",");

            if (nodeParas.length != methodParas.length)
                return false;

            for (int i = 0; i < nodeParas.length; i++) {
                if (!nodeParas[i].endsWith(methodParas[i]))
                    return false;
            }
        }
        return true;

    }

}
